//
// Created by bangsun on 2022/11/11.
//

#ifndef LOUTINE_PRIORITY_QUEUE_H
#define LOUTINE_PRIORITY_QUEUE_H

#include "util.h"
#include "deque.h"
#include <pthread.h>
#include <string.h>
#include <assert.h>

#define DEFAULT_CAPACITY (1<<6)

# define MIN_STEAL_NUM 5

#define COMPARE(a, b)  ((a)-(b))

/**
 * should be pthread save since mutex added
 */
struct priority_queue {
    volatile size_t capacity, size;
    volatile int positive_locker;

    pthread_mutex_t mutex;
    struct pq_node *data;

    deque tmp_queue;

};

struct pq_node {
    int prio;
    void *val;
};

typedef struct priority_queue priority_queue;
typedef struct pq_node pq_node;

priority_queue *create_pqueue();

void init_pqueue(priority_queue *pq);

void free_pqueue(priority_queue *pqueue);

int pqueue_add(priority_queue *pq, int prio, void *val);

void *pqueue_poll(priority_queue *pq);

void *pqueue_peek(priority_queue *pq);

void pqueue_print(priority_queue* pq);

/**
 * 从pq中抢夺指定比例的节点
 * @param ratio
 * @param robber
 */
void pq_steal_item(priority_queue *pq, priority_queue *target, float ratio);


#endif //LOUTINE_PRIORITY_QUEUE_H
